package logic

import (
	"api/pkg"
	"context"
	"net/http"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpLoadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpLoadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpLoadLogic {
	return &UpLoadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpLoadLogic) UpLoad(req *http.Request) (resp *types.Response, err error) {
	file, m, err := req.FormFile("image")
	if err != nil {
		return nil, err
	}
	load, err := pkg.UpLoad(file, m)
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}

	return &types.Response{
		Code:    200,
		Message: "图片发布成功",
		Data:    load,
	}, nil
}
